/*
开发思路:
1.首先获取小说的所有章节的url,保存到books数组里面;
2.循环获取小说每一个章节的html代码, 解析html,将文字内容与章节名字保存起来
3.保存小说内容到本地之前,先将小说保存到mongodb中
4.最后通过 fs 模块,将小说内容写入到本地硬盘中
*/
// 引入模块
var express = require('express');
// var superagent = require('superagent-charset');


// const request = require('superagent')
// require('superagent-charset')(request)


var http = require('http');


// var request = require('superagent');
var originRequest = require('request');

var iconv = require('iconv-lite');
var headers = {
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36'
}

var cheerio = require('cheerio');
// var async = require('async');
var router = express.Router();
var axios = require('axios');
var MongoClient = require('mongodb').MongoClient;
var fs = require('fs');
var qs = require('qs');
var URL = require('url');

// mongo
var DB_BOOKS = 'mongodb://localhost:27017/books'; // 数据库

// 所有文章内容集合
var chapter = [];

let index = 0;


// 小说内容字符串拼接
var books = ''
  // 章节链接
  // var link = 'http://m.boquge.com/wapbook/4043_2573590.html';
var link = 'http://m.boquge.com/wapbook/4043_146884663.html';




function request(url, callback) {
  var options = {
    url: url,
    encoding: null,
    //代理服务器
    //proxy: 'http://xxx.xxx.xxx.xxx:8888',
    headers: headers
  }
  originRequest(options, callback)
}
var url = 'http://m.boquge.com/wapbook/4043_146884663.html'
request(url, function(err, res, body) {
  var html = iconv.decode(body, 'gb2312')
  var $ = cheerio.load(html, {
    decodeEntities: false
  })
  console.log($('#cContent p').eq(0).html())
})


// 保存小说为TXT
router.get('/', function(req, res, next) {
  res.send('ok');

  // axios({
  //   method: 'get',
  //   url: link,
  //   headers: {
  //     'X-Requested-With': 'XMLHttpRequest'
  //   }
  // });



  // 删除数据库中已有的小说数据
  // _devare()
  // 执行爬虫程序

  // read()
  // axios.interceptors.request.use(function(request) {
  //   request['headers']['common']['Accept'] = 'application/json;charset=GBK;';
  //   return request;
  // }, function(error) {
  //   return Promise.reject(error);
  // });

  axios.get(link).then(resource => {
    var $ = cheerio.load(resource.data, {
      decodeEntities: false
    });
    // console.log($('#cContent p'))
    let P = $('#cContent p').eq(0).html();
    console.log(P)
      // iconv
      // console.log(iconv.decode(P));
    console.log(iconv.decode(resource.data, 'gb2312'));

    // var Abooks = []
    // for(let i=0;i<P.length;i++) {
    //   Abooks[i] = P.eq(i).html();
    //   if (i === P.length - 1) {
    //     res.send(Abooks);
    //   }
    // }

  })


});


function read１() {
  /*let time = setTimeout(() => {
    console.log(index)
    index++;
    if (index === 5) {
      res.send('程序终止！');
      clearInterval(time);
      console.log('程序终止！');
      return　res.send('程序终止！');
    } else {
      read()
      console.log('触发');
    }

/!*
    switch (index) {
      case 5:
        res.send('程序终止！');
        clearInterval(time);
        console.log(typeof read);
        console.log('程序终止！');
        break;
    }
*!/
}, 1000)*/


  var next = '';

  let pageTimers = setTimeout(() => {
    if (link.indexOf('cid') !== -1) {
      // 终结 setTimeout
      clearTimeout(pageTimers);
      console.log('ok')
        // if (next.indexOf('cid') === 18) {
        // if (index === 10) {


      // 保存到本地
      var path = '../Downloads/'; // 保存路径设置,这里之科协绝对路径
      // var text_name = path + books_name + '.txt'; // 保存文件路径已经名字
      var text_name = path + '阅读' + '.txt'; // 保存文件路径已经名字
      // fs.writeFile(本地文件, 数据源, 程序)
      for (let i = 0; i < chapter.length; i++) {

        // 写入章节内容
        var nlr = '';　 // 小说正文内容
        for (let j = 0; j < chapter[i][1].length; j++) {
          nlr = nlr + chapter[i][1][j] + '\r';
          // 当循环结束之后,保存小说内容到 books中
          if (j === chapter[i][1].length - 1) {
            books = books + chapter[i][0] + '\r' + nlr + '\r';
          }
        }
        if (i === chapter.length - 1) {
          // res.send(books)
          fs.writeFile(text_name, books, function(err) {
            if (err) {
              return console.error(err);
            }

            console.log("数据写入成功！");
            console.log("--------我是分割线-------------")
              // console.log("读取写入的数据！");
            fs.readFile(text_name, function(err, data) {
              if (err) {
                return console.error(err);
              }
              // console.log("异步读取文件数据: " + data.toString());
            });
          });
        }
      }
    } else {
      // 请求章节内容
      request.get(link).charset('gbk').end((err, sres) => {
        var html = sres.text;
        var $ = cheerio.load(html, {
          decodeEntities: false
        });
        var title = $('#content h1').eq(0).html(); //　章节目录
        // console.log(title);
        var content = $('#cContent p'); //　章节目录
        next = $('.pagebreak ul a').eq(3).attr('href'); //　下一章链接


        var contents = [];　 // 章节内容集合
        // 循环获取章节内容
        for (var i = 0; i < content.length; i++) {
          contents[i] = content.eq(i).html();
          if (i === content.length - 1) {
            link = 'http://m.boquge.com' + next;
            // console.log(link);

          }
        }
        chapter.push({
          contents
        })

        /*let a = {
          '0': '前言',
          '1': [
            '《银狐》是一本和《大宋的智慧》《唐砖》风格截然不同的小说，希望您会喜欢。',
            '十年磨一剑，《银狐》正在酝酿中，孑与2自己认为，这将是孑与写作的巅峰之作。',
            '写完了。我有一种砸键盘的冲动，欢喜的就像是小人。这不重要，兄弟们，我喜欢写作，喜欢讲故事……'
          ]
        };
        console.log(a[1])*/

        index++;
        read()

      });
    }
  }, 1000)
}


// 获取章节
router.get('/read', function(req, resource, next) {
  var params = URL.parse(req.url, true).query;
  var link = website + params.link;
  books_name = params.name // 小说保存本地的文件名
    // 进入该小说简介界面
  axios.get(link).then(res => {
    var $ = cheerio.load(res.data, {
      decodeEntities: false
    });
    // 获取指向小说目录的链接
    var read = $('div.top_t td a');
    // 获取小说目录每一个章节的url
    axios.get(pcWebsite + read[0].attribs.href).then(res => {
      // 通过 cheerio 来解析 html 代码
      var $ = cheerio.load(res.data, {
        decodeEntities: false
      });
      // 获取所有章节url的集合
      var chapter = $('dd a');
      // 获取所有章节的总数
      var booksArr = new Array(chapter.length)
        // 小说数组
      for (var i = 0; i < chapter.length; i++) {
        // substr   根据下标截取字符串
        // indexOf  返回指定字符的下标
        var chapterHref = chapter[i].children[0].parent.attribs.href
        booksArr[i] = chapterHref.substr(0, chapterHref.indexOf('.'));
      }
      // 排序已获得的章节:因为网页解析之后的章节顺序有误
      books = booksArr.sort(sortNumber);
      // 改变url链接位:http://www.bxwx9.org/b/36/36453/15220008.html
      for (var i = 0; i < books.length; i++) {
        books[i] = website + read[0].attribs.href + books[i] + '.html'
      }
      // 返回小说url信息
      resource.send(books)
    })
  })
});
// 下载章节
router.get('/download', function(req, resource, next) {
  // 删除数据库中已有的小说数据
  _devare()
    // 返回成功执行程序
  resource.send(true);
  var params = URL.parse(req.url, true).query;
  var reads = params.reads;
  var loopEnds = true;
  // console.log(loopEnds)
  var Fiction = new Array();

  // 定时器
  var y = 1;
  var x = 0;
  var z = books.length; // books.length
  page();

  function page() {
    var pageTimer = setTimeout(function() {
      console.log(books[x])
      if (x === z || books[x] === undefined) {
        var insertData = function(db, callback) {
          //连接到表 site
          var collection = db.collection('books');
          //插入数据
          collection.insert(Fiction, function(err, result) {
            if (err) {
              console.log('Error:' + err);
              return;
            }
            callback(result);
          });
        }
        MongoClient.connect(DB_BOOKS, function(err, db) {
          console.log("连接成功！");
          insertData(db, function(result) {
            // resource.send(result)
            console.log('数据成功保存到数据库中!');
            // 执行小说保存到本地的程序
            save();
            db.close();
          });
        });
        // 终结 setTimeout
        clearInterval(pageTimer);
      }
      // 请求小说某一个章节的正文,并在解析之后,将所有集合保存到数据库中
      axios.get(books[x]).then(res => {
        var $ = cheerio.load(res.data, {
          decodeEntities: false
        });
        var nr1s = {}
        var nr1 = $('div#nr1');
        var nr_title = $('div#nr_title');
        Fiction[x] = nr_title[0].children[0].data
        console.log(nr_title[0].children[0].data) // 小说章节
        for (var i = 0; i < nr1[0].children.length; i++) {
          var nr1Children = nr1[0].children[i].data
            // 筛选为空的段落
          if (nr1Children !== undefined) {
            nr1s[i] = nr1Children
          }
          // for 循环结束重新调用 page() ,来解析下一章的内容
          if (i === nr1[0].children.length - 1) {
            page();
          }
        }
        // 将获取到的章节内容,按顺序保存到 Fiction 中
        Fiction[x] = {
          nr_title: nr_title[0].children[0].data,
          nr1s: nr1s
        }
        x++;
      })
    }, 100)
  }

  // setInterval
  // var pageTimer = setInterval(function () {
  //   console.log(books[x])
  //   if (x === z || books[x] === undefined) {
  //     var insertData = function (db, callback) {
  //       //连接到表 site
  //       var collection = db.collection('books');
  //       //插入数据
  //       collection.insert(Fiction, function (err, result) {
  //         if (err) {
  //           console.log('Error:' + err);
  //           return;
  //         }
  //         callback(result);
  //       });
  //     }
  //     MongoClient.connect(DB_BOOKS, function (err, db) {
  //       console.log("连接成功！");
  //       insertData(db, function (result) {
  //         // resource.send(result)
  //         console.log('数据成功保存到数据库中!');
  //         db.close();
  //       });
  //     });
  //     // resource.send(Fiction)
  //     clearInterval(pageTimer);
  //   }
  //   axios.get(books[x]).then(res => {
  //     var $ = cheerio.load(res.data, {
  //       decodeEntities: false
  //     });
  //     var nr1s = {}
  //     var nr1 = $('div#nr1');
  //     var nr_title = $('div#nr_title');
  //     Fiction[x] = nr_title[0].children[0].data
  //     console.log(nr_title[0].children[0].data)
  //     for (var i = 0; i < nr1[0].children.length; i++) {
  //       var nr1Children = nr1[0].children[i].data
  //       if (nr1Children !== undefined) {
  //         nr1s[i] = nr1Children.substr(4)
  //       }
  //     }
  //     Fiction[x] = {
  //       nr_title: nr_title[0].children[0].data,
  //       nr1s: nr1s
  //     }
  //     x++;
  //   })
  // }, 2000)

  // for 循环获取小说章节
  /*  for (var j = 0; j < books.length; j++) {
  setTimeout(function () {
  axios.get(books[j]).then(res => {
  var $ = cheerio.load(res.data, {
  decodeEntities: false
});
if (j === books.length - 1) {
resource.send(Fiction)
}
var nr1s = {}
var nr1 = $('div#nr1');
var nr_title = $('div#nr_title');
Fiction[j] = nr_title[0].children[0].data
console.log(nr_title[0].children[0].data)
for (var i = 0; i < nr1[0].children.length; i++) {
var nr1Children = nr1[0].children[i].data
if (nr1Children !== undefined) {
nr1s[i] = nr1Children
}
}
Fiction[j] = {
nr_title: nr_title[0].children[0].data,
nr1s: nr1s
}
})
}, 2000)
}*/

  /* // 章节内容处理
  *   axios.get('http://m.bxwx9.org/b/22/22845/3973969.html').then(res=>{
  var $ = cheerio.load(res.data, {
  decodeEntities: false
  });
  var nr1s = {}
  var nr1 = $('div#nr1');
  var nr_title = $('div#nr_title');
  for (var i = 0; i < nr1[0].children.length; i++) {
  var nr1Children = nr1[0].children[i].data
  console.log()
  if (nr1Children !== undefined) {
  nr1s[i] = nr1Children
  }
  }
  console.log(nr1s)
  })
  * */

  // 计时器
  /*  var x = 0;
  var z = 22;
  const pageTimer = setInterval(function () {
  console.log((x === z - 1))
  if (x === z - 1) {
  clearInterval(pageTimer)
  }
  x++;
}, 1000)*/

  // if (loopEnds) {
  //   resource.send(Fiction)
  // }
})

// 搜索
router.get('/books', function(req, res, next) {
  var params = URL.parse(req.url, true).query;
  var html = [];
  var json = {
    searchkey: params.content,
    searchtype: 'articlename'
  };
  axios.post(website + '/modules/article/waps.php', qs.stringify(json)).then(resource => {
    var $ = cheerio.load(resource.data, {
      decodeEntities: false
    });
    // 小说作者  author
    var author = $('.author');
    // 最新章节 genxin
    var genxin = $('.genxin>a');
    // 状态 waps_full_0
    var waps_full_0 = $('.waps_full_0');
    // 小说类型 waps_sort
    var waps_sort = $('.waps_sort');
    // 小说名字  waps_name   a
    var waps_name = $('.waps_name>a');
    var book_length = 10;
    if (author.length < book_length) {
      book_length = author.length
    }
    for (var i = 0; i < book_length; i++) {
      html[i] = {
        "author": author[i].children[0].data.substr(3),
        "genxin": {
          "url": genxin[i].attribs.href,
          "status": genxin[i].children[0].data
        },
        "waps_full": waps_full_0[i].children[0].data,
        "waps_Name": waps_name[i].children[0].data,
        "waps_sort": $(waps_sort[i]).html(),
        "book_url": waps_name[i].attribs.href
      }
    }
    res.send(html)
  })
});

// 删除数据中的小说
router.get('/devare', function(req, res, next) {
  res.send(_devare()) // true/flase
});

// 保存小说为TXT
function save() {
  var selectData = function(db, callback) {
    //连接到表
    var collection = db.collection('books');
    //查询数据
    var whereStr = {};
    collection.find(whereStr).toArray(function(err, result) {
      if (err) {
        console.log('Error:' + err);
        return;
      }
      callback(result);
    });
  }

  MongoClient.connect(DB_BOOKS, function(err, db) {
    var books = '';
    selectData(db, function(result) {
      console.log('开始执行程序!')
        // 写入章节
      for (var i = 0; i < result.length; i++) {
        var nr1s = result[i].nr1s;
        // object 转 Array
        nr1s = Object.keys(nr1s).map(function(el) {
            return nr1s[el]
          })
          // 写入章节内容
        var nr1 = ''; // 小说正文内容
        for (var j = 0; j < nr1s.length; j++) {
          nr1 = nr1 + nr1s[j] + '\n'
            // 当循环结束之后,保存小说内容到 books中
          if (j === nr1s.length - 1) {
            books = books + result[i].nr_title + '\n' + nr1 + '\n';
          }
        }
        // 所有循环结束,执行小说数据写入本地程序
        if (i === result.length - 1) {
          console.log(books_name)
            // 保存到本地
          var path = '../Downloads/'; // 保存路径设置,这里之科协绝对路径
          // var text_name = path + books_name + '.txt'; // 保存文件路径已经名字
          var text_name = path + '阅读' + '.txt'; // 保存文件路径已经名字
          // fs.writeFile(本地文件, 数据源, 程序)
          fs.writeFile(text_name, books, function(err) {
            if (err) {
              return console.error(err);
            }
            console.log("数据写入成功！");
            // console.log("--------我是分割线-------------")
            // console.log("读取写入的数据！");
            fs.readFile(text_name, function(err, data) {
              if (err) {
                return console.error(err);
              }
              // console.log("异步读取文件数据: " + data.toString());
            });
          });
        }
      }
      db.close();
    });
  });
}

// 章节排序
function sortNumber(a, b) {
  return a - b
}

// 删除数据库中的小说
function _devare() {
  // 删除数据库 drop
  console.log('准备删除数据库!')
  var drop = function(db, callback) {
    db.collection('books').drop(function(err, result) {
      // console.log(result)
      callback(result);
    });
  }
  MongoClient.connect(DB_BOOKS, function(err, db) {
    drop(db, function(result) {
      db.close()
      console.log('删除数据库成功!')
      return '删除数据库成功!'
    })
  })
}

module.exports = router;